//
//  WFSettingWindow.m
//  谷居
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFSettingWindow.h"
#import "WFSettingCell.h"
#import "WFSettingUserCell.h"

#import "WFMessageVC.h"

#import "WFInspirationVC.h"
@interface WFSettingWindow()<UITableViewDataSource, UITableViewDelegate>

/** 触摸开始的位置坐标. */
@property (nonatomic, assign) CGFloat X;
@property (nonatomic, assign) CGFloat Y;

/** window 的点击dismiss. */
@property (nonatomic, retain) UITapGestureRecognizer *tap;

@property (nonatomic, retain) UITapGestureRecognizer *backTap;

@property (nonatomic, retain) NSMutableArray *picsArray;

@property (nonatomic, retain) NSMutableArray *textArray;

@property (nonatomic, assign) BOOL isPush;
//@property (nonatomic, retain) WFInspirationVC *vc;

@end

@implementation WFSettingWindow
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.windowLevel = UIWindowLevelAlert;
        
        [self createSetView];
    }
    return self;
}


- (void)createTableView {
    _tableVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _backgroudView.frame.size.width , _backgroudView.frame.size.height) style:UITableViewStylePlain];
    [_backgroudView addSubview:_tableVeiw];
    [_tableVeiw release];
    _tableVeiw.delegate = self;
    _tableVeiw.dataSource = self;
    
    
    _tableVeiw.backgroundColor = [UIColor colorWithRed:30 / 256.0 green:32 / 256.0 blue:40 / 256.0 alpha:1];

    /** 注册. */
    [_tableVeiw registerClass:[WFSettingCell class] forCellReuseIdentifier:@"setting"];
    [_tableVeiw registerClass:[WFSettingUserCell class] forCellReuseIdentifier:@"user"];
    _tableVeiw.separatorStyle = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WFSettingUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:30 / 256.0 green:32 / 256.0 blue:40 / 256.0 alpha:1];

        return cell;
    } else {
    WFSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setting" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:30 / 256.0 green:32 / 256.0 blue:40 / 256.0 alpha:1];
        cell.leftImage.image = self.picsArray[indexPath.row - 1];
        cell.rightLabel.text = self.textArray[indexPath.row - 1];
        cell.rightLabel.textColor = [UIColor lightTextColor];

    return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    } else {
        return 80;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismiss];
    switch (indexPath.row) {
        case 0:
            NSLog(@"点击第一个. ");
            break;
        case 1:
            NSLog(@"点击第二个. ");
            WFMessageVC *massageVC = [[WFMessageVC alloc] init];
            
            [self.delegate push:massageVC];
            [massageVC release];
            break;
        case 2:
            NSLog(@"点击地阿三哥. ");
            
            [self.delegate pushLikeCollectionVC];
            
    
            break;
        case 3: {
            WFInspirationVC *vc = [[WFInspirationVC alloc] init];
            [self.delegate push:vc];
            [vc release];
            break;
        }
        case 4:{
            WFInspirationVC *vc = [[WFInspirationVC alloc] init];
            [self.delegate push:vc];
            [vc release];

            break;
        }
        case 5:
            [self.delegate pushSetVC];
            break;
         default:
            break;
    }
}



- (void)createSetView {
    _backgroudView = [[UIView alloc] initWithFrame:CGRectMake(- SCREEN_WIDTH /3 * 2, 0, SCREEN_WIDTH / 3 * 2, [[UIScreen mainScreen]bounds].size.height)];
    [self addSubview:_backgroudView];
    [_backgroudView release];
    _backgroudView.backgroundColor = [UIColor colorWithRed:30 / 256.0 green:32 / 256.0 blue:40 / 256.0 alpha:1];
    [self createTableView];
    [self createData];
}


- (void)createData {
    self.textArray = @[@"消息中心", @"我的喜欢", @"我的灵感集", @"推荐给APP好友", @"系统设置"].mutableCopy;
    for (int i = 1; i < 6; i++) {
        NSString *str = [NSString stringWithFormat:@"%d.png", i];
        UIImage *image = [UIImage imageNamed:str];
        NSLog(@"%@", image);
        [self.picsArray addObject:image];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)picsArray {
    if (_picsArray == nil) {
        _picsArray = [[NSMutableArray array] retain];
    }
    return _picsArray ;
}
- (NSMutableArray *)textArray {
    if (_textArray == nil) {
        _textArray = [[NSMutableArray array]retain];
    }
    return _textArray ;
}
- (void)dealloc {
#warning array release
    [super dealloc];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    [self dismiss];
}

- (void)show {
    [self makeKeyAndVisible];
    [UIView animateWithDuration:0.5 animations:^{
        _backgroudView.center = CGPointMake(SCREEN_WIDTH / 3.0, SCREEN_HEIGHT / 2.0);
        UIColor *color = [UIColor blackColor];
        self.backgroundColor = [color colorWithAlphaComponent:0.5];
        
    } completion:^(BOOL finished) {
        
        
    }];
}

- (void)dismiss {

    [UIView animateWithDuration:0.5 animations:^{
        _backgroudView.frame = CGRectMake(-_backgroudView.frame.size.width, 0, _backgroudView.frame.size.width, _backgroudView.frame.size.height);
//        self.backgroudView = 
        
    } completion:^(BOOL finished) {
        
        self.hidden = YES;
    }];
    [self resignKeyWindow];
}





@end
